﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Drawing;
using System.Collections.Generic;

public partial class admin_TimKiemNganhTrung : System.Web.UI.UserControl
{
    #region "Member variables"
    DA_DM_TRUONG objectDA_DM_TRUONG;
    DA_NGANH_THUOC_TRUONG objDA_NGANH_THUOC_TRUONG;
    DA_GD_NGANH_TRUNG objDA_GD_NGANH_TRUNG;
    #endregion
    #region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion
    #region "Private methods"
    private void load_danh_sach_truong()
    {
        DataSet ds_DA_DM_TRUONG = objectDA_DM_TRUONG.load_danh_sach_truong(m_txt_ten_truong.Text.Trim());
        for (int i = 0; i < ds_DA_DM_TRUONG.Tables[0].Rows.Count; i++)
        {
            this.lbTruong.Items.Add(new ListItem(ds_DA_DM_TRUONG.Tables[0].Rows[i]["TEN_TRUONG"].ToString(), ds_DA_DM_TRUONG.Tables[0].Rows[i]["ID"].ToString()));
            
        } 
    }
    private void load_dm_nganh_trung()
    {
        if (lbTruong.SelectedItem == null) return;
        int m_i_ds_nganh_trung = Convert.ToInt32(this.lbTruong.SelectedItem.Value);
        DataSet ds_DA_NGANH_THUOC_TRUONG = objDA_NGANH_THUOC_TRUONG.load_danh_sach_nganh(m_i_ds_nganh_trung, m_txt_ten_nganh.Text.Trim());
        for (int i = 0; i < ds_DA_NGANH_THUOC_TRUONG.Tables[0].Rows.Count; i++)
        {
            this.lbNganh.Items.Add(new ListItem(ds_DA_NGANH_THUOC_TRUONG.Tables[0].Rows[i]["TEN_NGANH"].ToString(), ds_DA_NGANH_THUOC_TRUONG.Tables[0].Rows[i]["ID"].ToString()));
        }

    }
    private void hien_thi_danh_muc_nganh_trung()
    {
        int m_i_dm_nganh_trung = Convert.ToInt32(this.lbNganh.SelectedItem.Value);
        DataSet ds_DA_NGANH_TRUNG = objDA_GD_NGANH_TRUNG.load_danh_sach_nganh_trung(m_i_dm_nganh_trung);
        grvNganhTrung.DataSource = ds_DA_NGANH_TRUNG.Tables[0];
        grvNganhTrung.DataBind();
    }
  
    #endregion
    #region "Events handle"
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objectDA_DM_TRUONG = new DA_DM_TRUONG(sqlConnec);
        objDA_NGANH_THUOC_TRUONG = new DA_NGANH_THUOC_TRUONG(sqlConnec);
        objDA_GD_NGANH_TRUNG = new DA_GD_NGANH_TRUNG(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
        loadDataAccess();
        if (!Page.IsPostBack)
        {
            if ((lbTruong.SelectedIndex == -1)&&(lbNganh.Items.Count == 0))
            {
                lbNganh.Items.Add("- Chọn một trường để hiển thị ngành của trường -");
            }
           load_danh_sach_truong();          
        }
    }
    protected void btnTimTruong_Click(object sender, EventArgs e)
    {
        try
        {
            //lbTruong.Items[0].Value = "khoitao";//neu du lieu trong thi ko bi loi xay ra
            this.lbTruong.Items.Clear();
            load_danh_sach_truong();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    protected void lbTruong_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            m_txt_ten_nganh.Text = "";
            this.lbNganh.Items.Clear();           
            load_dm_nganh_trung();
            
        }
        catch (Exception v_e)
        {

            throw v_e;
        }
    }
    protected void btnTimNganh_Click(object sender, EventArgs e)
    {
        try
        {
            m_thong_bao.Text = "";
            this.lbNganh.Items.Clear();
            load_dm_nganh_trung();
        }
        catch (Exception v_e)
        {

            throw v_e;
        }

    }
    protected void m_cmd_hien_thi_nganh_trung_Click(object sender, EventArgs e)
    {
        try
        {
            if (
                (lbTruong.SelectedIndex == -1 )||
                (lbTruong.SelectedIndex != -1 && lbNganh.SelectedIndex == -1)
                ){
                lbNganh.Items.Clear();
                grvNganhTrung.Visible = false;
                m_thong_bao.Text = "Bạn hãy chọn trường và ngành tương ứng";
            }
            else
            {
                m_thong_bao.Text = "";
                grvNganhTrung.Visible = true;
                hien_thi_danh_muc_nganh_trung();
            }
        }
        catch (Exception v_e)
        {
            
            throw v_e;
        }
    }
    #endregion  
}
